Add option "DATUM" to xcsv format.
authoroliskoli <oliskoli>
Sat, 2 Dec 2006 20:37:13 +0000 (20:37 +0000)
committeroliskoli <oliskoli>
Sat, 2 Dec 2006 20:37:13 +0000 (20:37 +0000)
csv_util.c
csv_util.h
xcsv.c
xmldoc/chapters/styles.xml
xmldoc/formats/options/xcsv-datum.xml [new file with mode: 0644]

index b92ebeb1389214b33fdd24b30f77625c97b151a5..c0828747530694995581e20a6add7dfcb875dfe6 100644 (file)
@@ -25,6 +25,7 @@
 #include "csv_util.h"
 #include "grtcirc.h"
 #include "strptime.h"
+#include "jeeps/gpsmath.h"
 
 #define MYNAME "CSV_UTIL"
 
@@ -38,6 +39,7 @@
 #define EXCEL_TO_TIMET(a) ((a - 25569.0) * 86400.0)
 #define TIMET_TO_EXCEL(a) ((a / 86400.0) + 25569.0)
 
+#define GPS_DATUM_WGS84                118
 
 /****************************************************************************/
 /* obligatory global struct                                                 */
@@ -583,6 +585,7 @@ xcsv_file_init(void)
     xcsv_file.type = ff_type_file;
 
     xcsv_file.mkshort_handle = mkshort_new_handle();
+    xcsv_file.gps_datum = GPS_DATUM_WGS84;
 }
 
 /*****************************************************************************/
@@ -1055,6 +1058,11 @@ xcsv_data_read(void)
                 s = csv_lineparse(NULL, xcsv_file.field_delimiter, "",
                   linecount);
             }
+            if ((xcsv_file.gps_datum > -1) && (xcsv_file.gps_datum != GPS_DATUM_WGS84)) {
+               double alt;
+               GPS_Math_Known_Datum_To_WGS84_M(wpt_tmp->latitude, wpt_tmp->longitude, 0.0,
+                   &wpt_tmp->latitude, &wpt_tmp->longitude, &alt, xcsv_file.gps_datum);
+           }
             waypt_add(wpt_tmp);
         }
 
@@ -1084,13 +1092,14 @@ xcsv_waypt_pr(const waypoint *wpt)
     int i;
     field_map_t *fmp;
     queue *elem, *tmp;
+    double latitude, longitude;
     
     if ( oldlon < 900 ) {
        pathdist += radtomiles(gcdist(RAD(oldlat),RAD(oldlon),
                        RAD(wpt->latitude),RAD(wpt->longitude)));
     }
-    oldlon = wpt->longitude;
-    oldlat = wpt->latitude;
+    longitude = oldlon = wpt->longitude;
+    latitude = oldlat = wpt->latitude;
 
     if (xcsv_file.field_delimiter && strcmp(xcsv_file.field_delimiter, "\\w") == 0)
         write_delimiter = " ";
@@ -1130,12 +1139,17 @@ xcsv_waypt_pr(const waypoint *wpt)
            description = xstrdup(odesc);
            xfree(odesc);
     }
+    if ((xcsv_file.gps_datum > -1) && (xcsv_file.gps_datum != GPS_DATUM_WGS84)) {
+       double alt;
+       GPS_Math_WGS84_To_Known_Datum_M(latitude, longitude, 0.0,
+           &latitude, &longitude, &alt, xcsv_file.gps_datum);
+    }
 
     i = 0;
     QUEUE_FOR_EACH(xcsv_file.ofield, elem, tmp) {
        char *obuff;
-       double lat = wpt->latitude;
-       double lon = wpt->longitude;
+       double lat = latitude;
+       double lon = longitude;
        /*
         * A klunky concept.   This should evaluate to true for any
         * field if we think we don't have realistic value for it.
index 8da2d3e2a89896a91885aea27d40d37a45d6a2df..cfb0eaaa2c283616aa7fe070f054aee1baacbff4 100644 (file)
@@ -131,7 +131,9 @@ typedef struct {
     
     short_handle mkshort_handle;/* handle for mkshort() */
     ff_type type;              /* format type for GUI wrappers. */
-    
+
+    int gps_datum;             /* result of GPS_Lookup_Datum_Index */
+
 } xcsv_file_t;
 
 
diff --git a/xcsv.c b/xcsv.c
index 6c246c4c564c982cca12bc4c4e3aa2224ddc151a..87cb0ead5c7a3b526f710c9a76dab625faf11d98 100644 (file)
--- a/xcsv.c
+++ b/xcsv.c
@@ -26,6 +26,7 @@
 #include <ctype.h>
 #include "defs.h"
 #include "csv_util.h"
+#include "jeeps/gpsmath.h"
 
 #if CSVFMTS_ENABLED
 #define MYNAME "XCSV"
@@ -38,6 +39,7 @@ static char *snupperopt = NULL;
 static char *snuniqueopt = NULL;
 char *prefer_shortnames = NULL;
 char *xcsv_urlbase = NULL;
+static char *opt_datum;
 
 static const char *intstylebuf = NULL;
 
@@ -58,6 +60,8 @@ arglist_t xcsv_args[] = {
        {"prefer_shortnames", &prefer_shortnames, 
                "Use shortname instead of description", 
                NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
+       {"datum", &opt_datum, "GPS datum (def. WGS 84)", 
+               NULL, ARGTYPE_STRING, ARG_NOMINMAX}, 
        ARG_TERMINATOR
 };
 
@@ -327,6 +331,13 @@ xcsv_parse_style_line(const char *sbuff)
            cet_convert_init(p, 1);
            xfree(p);
        } else
+
+       if (ISSTOKEN(sbuff, "DATUM")) {
+           p = csv_stringtrim(&sbuff[8], "\"", 1);
+           xcsv_file.gps_datum = GPS_Lookup_Datum_Index(p);
+           is_fatal(xcsv_file.gps_datum < 0, MYNAME ": datum \"%s\" is not supported.", p);
+           xfree(p);
+       } else
        
        if (ISSTOKEN(sbuff, "IFIELD")) {
            key = val = pfc = NULL;
@@ -515,6 +526,10 @@ xcsv_rd_init(const char *fname)
     }
 
     xcsv_file.xcsvfp = gbfopen(fname, "r", MYNAME);
+    if (opt_datum) {
+       xcsv_file.gps_datum = GPS_Lookup_Datum_Index(opt_datum);
+       is_fatal(xcsv_file.gps_datum < 0, MYNAME ": datum \"%s\" is not supported.", opt_datum);
+    }
 
 }
 
@@ -564,7 +579,10 @@ xcsv_wr_init(const char *fname)
         setshort_badchars(xcsv_file.mkshort_handle, xcsv_file.badchars);
 
     }
-
+    if (opt_datum) {
+       xcsv_file.gps_datum = GPS_Lookup_Datum_Index(opt_datum);
+       is_fatal(xcsv_file.gps_datum < 0, MYNAME ": datum \"%s\" is not supported.", opt_datum);
+    }
 }
 
 static void
index e471f74c75a098eccbc9d0cd0d50149aec7c1f8c..c4019f9a60cd6977e879b49580dea7c11abe3daa 100644 (file)
@@ -210,6 +210,15 @@ must be one listed by 'gpsbabel -l'.    example:
 <screen format="linespecific">   ENCODING             UTF-8    # Use UTF-8 for input and output.
 </screen>
 </section>
+<section id="style_global_datum">
+<title>DATUM</title>
+<para>
+This value specifies the GPS datum to be used on read or write. Valid values for this 
+option are listed in <xref linkend="Datums" />.
+</para>
+<screen format="linespecific">   DATUM             European 1950
+</screen>
+</section>
 </section> <!-- global -->
 
 <section id="style_behavior">
diff --git a/xmldoc/formats/options/xcsv-datum.xml b/xmldoc/formats/options/xcsv-datum.xml
new file mode 100644 (file)
index 0000000..2651994
--- /dev/null
@@ -0,0 +1,4 @@
+<para>
+This option specifies the GPS datum to be used on read or write. Valid values for this 
+option are listed in <xref linkend="Datums" />.
+</para>